package git.matrix.controller;


import git.matrix.pojo.Page;
import git.matrix.pojo.Role;
import git.matrix.pojo.User;
import git.matrix.service.RoleService;
import git.matrix.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Currency;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired //注入业务 角色模块
    RoleService roleService ;

    //测试查询所有用户信
    @RequestMapping("/userAll")
    public String getAllUser(HttpServletRequest request){
       List<User> allUser = userService.getAllUser();
        request.setAttribute("allUser",allUser);
        System.out.println(" 测试查询所有用户信息");
        return "view/userAll";
    }



    //综合查询 查询用户信息对应角色
    @RequestMapping("/getUserRole")
    public String getUserRole(Model model, HttpSession session,
                              HttpServletRequest request){
        System.out.println(" 查询用户信息对应角色");
        List<User> userRole = userService.getUserRole();
        model.addAttribute("userRole", userRole);
        System.out.println("userRole = " + userRole);
        return "view/userRole";
    }





    /**
     *  分页+用户角色+多条件查询
     * @return 用户信息
     */


    @RequestMapping(value = "/getAllUser",method = RequestMethod.GET)

    public String getAllUser(Model model,HttpSession session,
                             HttpServletRequest request){
        //取出当前用户
       User user = (User) session.getAttribute("userOnLogin");
        System.out.println("取出当前用户"+"user = " + user);

       // 判断登录用户角色 普通用户不能进行查询
       if (user.getUserRole()==3){
           return "redirect:/frame";
       }

       //从表单中获取用户名+用户角色
       String userName = (String) request.getAttribute("queryName");
       String  userRole = (String) request.getAttribute("queryUserRole");

        System.out.println("---测试---"+"userName = " + userName);
        System.out.println("---测试---"+"userRole = " + userRole);
        //根据用户输入的条件查询
        int userRoleId =0;
        if(userName!=null&&userName.equals(""))//判断用户名
        {
            request.setAttribute("queryName", userName);
        }
        if(userRole!=null&&userRole.equals(""))//判断角色
        {
            userRoleId = Integer.parseInt(userRole);//用户查询的角色ID

            request.setAttribute("queryUserRole", userRoleId);
        }



        //设置分页显示
        Page page = new Page();
        page.setSize(5);//设置每页显示5
        String currentPage = request.getParameter("currentPage");//当前页
        int pageCount = 0;
       try {
           if (currentPage != null && currentPage.equals(""))// currentPage
           // ==null “”
           {
               pageCount = Integer.parseInt(currentPage);
           }
           pageCount = 1;
       }catch (Exception e){
           e.printStackTrace();
           pageCount = 1;
       }
        page.setCurrentPage(pageCount);//设置当前页面

        List<User> data = userService.getUserByPage(userName,userRoleId,page);//根据用户名分页查询

        model.addAttribute("userList", data);
        //获取用户角色
        List<Role> roleList = roleService.getRoleList();
        model.addAttribute("roleList", roleList);
        //添加页码信息
        request.setAttribute("currentPage", pageCount);
        request.setAttribute("totalCount", page.getTotalCount());
        int Count = (page.getTotalCount())/page.getSize()+1;
        request.setAttribute("totalPage",Count);
        
        return "user/userList";
    }
    /**
     * 添加用户
     * @return
     */
    @RequestMapping("/useradd")
    public String userAdd(){
        return "user/useradd";
    }

    /**
     * 用户详细信息
     */
    @RequestMapping("viewUser.do")
public String viewUser(){
    return "user/userview";
}

}
